/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package unc.pds.facets;

import unc.pds.auth.IPDSSecurity;
import unc.pds.auth.SecurityBRemote;
import unc.pds.data.Consts;
import unc.pds.model.CommentRemote;
import unc.pds.model.Commentable;
import unc.pds.model.DiaryRemote;

/**
 *
 * @author Mikhail
 */
public class CommentsFacet implements Facet {
    public CommentsFacet(Commentable post, IPDSSecurity security) {
        this.post = post;
        this.security = security;
    }
    private void drawComments(Commentable post, int padding) throws Exception {
        CommentRemote[] comments = post.getComments();
        if (comments!=null) {
            for (int i=0; i<comments.length; i++) {
                result+="<div style=\"padding-left: "+padding+"px\"><p>";
                if (additionAllowed) {
                    result+="<input type=\"radio\" name=\"postid\" value=\""+comments[i].getKey()+"\"/>";
                }
                result+="<b>"+comments[i].getAuthor().getNickname()+"</b> - <i>"+DateFormatter.format(comments[i].getDate())+"</i></p>\n";
                result+="<p>"+comments[i].getComment()+"</p></div>\n";
                drawComments(comments[i],padding+10);
            }
        }
    }
    private IPDSSecurity security;
    private boolean additionAllowed=false;
    private Commentable post;
    private String result="";
    public String draw() throws Exception {
        additionAllowed = security.getCrPermission(post.getKey(), Consts.OBJECT_TYPE__COMMENT);
        result += "<h4>Оставить комментарий:</h4>\n";
        if (additionAllowed) {
            result += "<form action=\"comment-processor.jsp\" method=\"post\" onsubmit=\"return validateEmpty('content');\">\n";
            result += "<p><input type=\"radio\" name=\"postid\" checked value=\""+post.getKey()+"\"/>Присоединить к сообщению</p>\n";
            result += "<input type=\"hidden\" name=\"title\" value=\"JAFC\"/>\n";
            result += "<p><textarea name=\"content\" cols=\"30\" rows=\"5\" id=\"content\"></textarea></p>\n";
            result += "<p><input type=\"submit\" value=\"Отправить\"/></p>\n";
        }
        result += "<h4>Комментарии:</h4>\n";
        drawComments(post,0);
        if (additionAllowed) {
            result += "</form>\n";
        }
        return result;
    }
    public boolean isSide() {
        return false;
    }
}
